<template>
    <el-dialog :title='this.showtittle' :visible="showDialog" @close="close" width="600px">
        <el-form :model="formData" :rules="formRules" ref="formData" label-width="120px">
          <el-form-item label="部门名称:" prop="name">
            <el-input v-model="formData.name" style="width: 80%;" size="mini" placeholder="2-10个字符" ></el-input>
          </el-form-item>
          <el-form-item label="部门编码:" prop="code">
            <el-input v-model="formData.code" style="width: 80%;" size="mini" placeholder="2-10个字符"></el-input>
          </el-form-item>
          <el-form-item label="部门负责人:" prop="managerId">
            <el-select v-model="formData.managerId" style="width:80%;" size="mini" placeholder="请选择负责人">
                <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="部门介绍:" prop="introduce">
            <el-input type="textarea" :rows="4" v-model="formData.introduce" style="width: 80%;" size="mini" placeholder="1-100个字符"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="btnOk" size="mini">确 定</el-button>
          <el-button @click="btnCannel" size="mini">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
import {getDepartment,getManagerIdAndName,addDepartment,getDepartmentDetail,updateDepartment} from '@/api/department'
export default {
    name:'AddDept',
    props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    },
    
  },
  data(){
    return{
      //弹框数据
      formData:{
        name:'', //名称
        code:'', //编码
        managerId:'',//负责人
        introduce:'',//介绍
        pid:'' //父id
      },
      //弹框的校验规则
      formRules:{
            //名称
        name:[{required:true,message:'部门名称不能为空',trigger: 'blur'}
            ,{min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'}
            ,{trigger: 'blur',validator:async(rule ,value ,callback)=>{
              debugger
             let result = await getDepartment()
                if(this.formData.id){
                  result = result.filter(item => item.id!==this.formData.id)
                }
                if(result.some(item=> item.name===value)){
                    callback(new Error('部门中已经有该名称了'))
                }else{
                    callback()
                }
            }}],
            //编码
        code:[{required:true,message:'部门编码不能为空',trigger: 'blur'}
            ,{min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'}
            ,{trigger: 'blur',validator:async(rule ,value ,callback)=>{
             let  result = await getDepartment()
                if(this.formData.id){
                  result = result.filter(item => item.id !== this.formData.id)
                }
                if(result.some(item=> item.code===value)){
                    callback(new Error('部门中已经有该编码了'))
                }else{
                    callback()
                }
            }}],
            //负责人
        managerId:[{required:true,message:'部门负责人不能为空',trigger: 'change'}],
            //介绍
        introduce:[{required:true,message:'部门介绍不能为空',trigger: 'blur'}
            ,{min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'}]
      },      
      //添加下拉框
      managerList:[]
    }
  },
  methods: {
    //弹框的叉
    close() {
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      }
      this.$refs.formData.resetFields() //重置
      this.$emit('update:showDialog', false)
    },
     //添加下拉框
    async getManagerIdAndName(){
     const result =  await getManagerIdAndName()
     this.managerList = result
    },
    //弹框的确认按钮
    btnOk(){
      this.$refs.formData.validate(async(isOk)=>{
        if(isOk){
          let msg = '新增'
          debugger
          if(this.formData.id){
            msg = '编辑'
            await updateDepartment(this.formData)
          }else{
            console.log(this.formData)
            this.formData.pid =this.currentNodeId
            await addDepartment(this.formData)
          }
          this.$emit("updateDepartment")
          this.btnCannel()
          this.$message.success(`${msg}`+'添加成功')
        }
      })
    },
    //取消按钮
    btnCannel(){
      this.close()
    },
    //部门详情
    async getDepartmentDetail(){
    this.formData = await getDepartmentDetail(this.currentNodeId)
    }
  },
  created(){
    //下拉框
    this.getManagerIdAndName()
  },
  computed:{
    showtittle(){
      return this.formData.id?'编辑部门':'新增部门'
    }
  }
}
</script>
